package com.litong.litongjiaoyu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.controller.WxPushServiceQcl;
import com.litong.litongjiaoyu.dao.ReservationDao;
import com.litong.litongjiaoyu.entity.SysReservation;
import com.litong.litongjiaoyu.entity.WxMaTemplateData;
import com.litong.litongjiaoyu.entity.WxMssVo;
import com.litong.litongjiaoyu.service.CommenService;
import com.litong.litongjiaoyu.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReservationServiceImpl
 * @Description 课程预约服务层具体实现
 * @Author jingz
 * @Date 2019/12/1 15:30
 * @Version 1.0
 **/

@Service
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationDao reservationDao;

    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;
    @Autowired
    private CommenService commenService;


    private String Template_id = "83C_jGnmxdoECGA4Tm39Wi4ZHY4i9APemnnD4NnfIyY";

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public List<SysReservation> query() {
        return reservationDao.query();
    }

    /**
     * 分类查询（主要用于区分预约和试听）
     *
     * @param types
     * @return
     */
    @Override
    public List<SysReservation> classification(String types, String campus) {
        return reservationDao.classification(types, campus);
    }
    
    @Override
    public List<SysReservation> getFaceToFaceAppointmentRecords(String phone) {
        return reservationDao.getFaceToFaceAppointmentRecords(phone);
    }
    
    /**
     * 添加预约
     *
     * @param sysReservation
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add(SysReservation sysReservation) throws Exception {

        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

//      预约人
        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(sysReservation.getName());
        data.put("name1", wxMaTemplateData1);

//      预约时间
        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue(sysReservation.getDate() + " " + sysReservation.getTime());
        data.put("date2", wxMaTemplateData2);

//      预约课程
        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();
        wxMaTemplateData3.setValue(sysReservation.getCourse());
        data.put("thing3", wxMaTemplateData3);
//      门店地址
        WxMaTemplateData wxMaTemplateData4 = new WxMaTemplateData();
        wxMaTemplateData4.setValue(sysReservation.getAddress());
        data.put("thing4", wxMaTemplateData4);

//      设置接收人、模板id，appid，appSecret
        WxMssVo wxMssVo = new WxMssVo();

        int a = reservationDao.add(sysReservation);

        if (a > 0){
            wxMssVo.setTouser(sysReservation.getOpenid());

            wxMssVo.setPage("pages/oto/oto");
            wxMssVo.setData(data);
            wxMssVo.setTemplate_id(Template_id);
            //        获取消息发送结果
            JSONObject result = wxPushServiceQcl.pushOneUser("wx6a41faf108b8ecab", "6e9c02ef2d83dcd91a2f0e730a9aa358", wxMssVo);

            return a;

        } else {
            return a;
        }
    }

    /**
     * 修改内容
     * 后期加入用户推送状态提醒
     * @param reservation
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(SysReservation reservation) throws Exception {

        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

//      预约人
        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(reservation.getName());
        data.put("name1", wxMaTemplateData1);

//      到店时间
        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue(reservation.getDate() + " " + reservation.getTime());
        data.put("time5", wxMaTemplateData2);

//      预约课程
        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();
        wxMaTemplateData3.setValue(reservation.getCourse());
        data.put("thing4", wxMaTemplateData3);
//      预约状态和备注
        WxMaTemplateData wxMaTemplateData4 = new WxMaTemplateData();
        WxMaTemplateData wxMaTemplateData5 = new WxMaTemplateData();

        if (reservation.getStatus() == 1) {
            wxMaTemplateData4.setValue("预约成功");
            wxMaTemplateData5.setValue("请带好上课用具在预约时间之前到达");
        } else {
            wxMaTemplateData4.setValue("预约失败");
            wxMaTemplateData5.setValue("请联系老师询问原因");
        }

        data.put("phrase2", wxMaTemplateData4);
        data.put("thing3", wxMaTemplateData5);

//      设置接收人、模板id，appid，appSecret
        WxMssVo wxMssVo = new WxMssVo();

        int a = reservationDao.update(reservation);

        if (a > 0){
//          此处获取的openid错误，当前是老师的openid，应该获取学生的openid
            wxMssVo.setTouser(commenService.getOpenId(reservation.getPhone()));

            wxMssVo.setPage("pages/index/index");
            wxMssVo.setData(data);
            wxMssVo.setTemplate_id("Q9mPB-htYA-6Zw6v7FzNtBcg6H1ipoX-QKlpODXTXTA");
            //        获取消息发送结果
            JSONObject result = wxPushServiceQcl.pushOneUser("wx78763b2bea2646c2", "571a3836b6b557d2e7dc1aaeabdc0be2", wxMssVo);

            return a;

        } else {
            return a;
        }
    }

    /**
     * 删除全部
     *
     * @return
     */
    @Transactional
    @Override
    public int deleteAll() {
        return reservationDao.deleteAll();
    }
}
